#include "World.h"
#include "Observer.h"
#include "Application.h"
#include "hgeFont.h"
#include "GameObject.h"
#include "Genome.h"
#include "GeneticsEngine.h"
#include "Entity.h"
#include "Track.h"

World* Singleton<World>::m_pInstance = NULL;

World::World()
: m_pText(NULL)
, m_textBuffer(NULL)
{
	//Attach this class to the observer to receive broadcasts.
	Observer::GetInstance().AttachObserver(this);
}

World::~World()
{
	Cleanup();
}
void World::Initiate()
{
	m_textBuffer = new char[512];
	m_pText = new hgeFont("media/font1.fnt");

	m_pGraphBackground = new GameObject();
	m_pGraphBackground->CreateObject("GraphBackground", 560, 47);
	m_pGraphBackground->SetPos(15,10);
	m_pGraphBackground->GetSprite()->SetBlendMode(4);
	//m_pGraphBackground->GetSprite()->SetColor(hgeColor(255,255,255,128).GetHWColor());

	m_pGraphNeedle = new GameObject();
	m_pGraphNeedle->CreateObject("GraphNeedle", 2, 21);
	m_needlePos = 15;

	for(int i=0; i<160; ++i)
	{
		//Father
		GameObject* pObject = new GameObject();
		pObject->CreateObject("GraphNode",2,2);
		pObject->SetPos((i*3)+45, 30);
		m_fatherGraphNodes.push_back(pObject);
	}
}
void World::Notify(const Event* a_event)
{
	//Only one event posted at one time, return after the proper event has been found.
	Observer* pObserver = &Observer::GetInstance();
	if(a_event->id == pObserver->Hash("W Key"))
	{
		return;
	}
}

void World::Update(float a_dt)
{
	m_pGraphNeedle->Update(a_dt);
	m_needlePos = Application::GetInstance().GetGeneticsEngine()->GetTrack()->GetNeedlePos();
	m_pGraphNeedle->SetPos(m_needlePos, 10);

	if(Application::GetInstance().GetHGE()->Input_GetKeyState(HGEK_P))
	{
		Application::GetInstance().GetGeneticsEngine()->Pause(true);
	}
	else if(Application::GetInstance().GetHGE()->Input_GetKeyState(HGEK_O))
	{
		Application::GetInstance().GetGeneticsEngine()->Pause(false);
	}
  if(Application::GetInstance().GetHGE()->Input_GetKeyState(HGEK_S))
    Application::GetInstance().GetGeneticsEngine()->GetTrack()->SetRenderSpline(true);
  else if(Application::GetInstance().GetHGE()->Input_GetKeyState(HGEK_A))
    Application::GetInstance().GetGeneticsEngine()->GetTrack()->SetRenderSpline(false);
	//get the braking value, clamp it 0 to one, and put it here
	Entity* pCurEntity = Application::GetInstance().GetGeneticsEngine()->GetFather();
	//Make sure theres a valid entity
	if(pCurEntity)
	{
		std::vector<Genome> genes = pCurEntity->GetAllGenomes();
		if(!genes.empty())
		{
			for(int i=0; i<160; ++i)
			{
				m_fatherGraphNodes.at(i)->SetPos(m_fatherGraphNodes.at(i)->GetPos().x, 20+genes.at(i).m_brakeValue*10);
				m_fatherGraphNodes.at(i)->Update(a_dt);
			}
		}
	}
}
void World::Render(float a_dt)
{
	m_pGraphBackground->Update(a_dt);
	m_pGraphBackground->Render();
	
	m_pGraphNeedle->Render();
	for(int i=0; i<160; ++i)
	{
		m_fatherGraphNodes.at(i)->Update(a_dt);
		m_fatherGraphNodes.at(i)->Render();
	}
}

void World::Cleanup()
{
	if(m_pText)
	{
		delete m_pText;
		m_pText = NULL;
	}
}